package com.cp.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cp.common.exception.Assert;
import com.cp.common.result.ResponseEnum;
import com.cp.core.hfb.FormHelper;
import com.cp.core.hfb.HfbConst;
import com.cp.core.hfb.RequestHelper;
import com.cp.core.mapper.UserBindMapper;
import com.cp.core.pojo.entity.UserBind;
import com.cp.core.pojo.entity.UserInfo;
import com.cp.core.pojo.vo.UserBindVO;
import com.cp.core.service.UserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cp.core.service.UserInfoService;
import feign.hystrix.FallbackFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.cp.core.enums.UserBindEnum;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author ming
 * @since 2022-07-27
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {
    @Resource
    private UserInfoService userInfoService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String commitBindUser(UserBindVO userBindVO, Long userId) {
        //相同身份证号不允许,不同的user_id
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card",userBindVO.getIdCard()).ne("user_id",userId);
        Integer count = this.baseMapper.selectCount(wrapper);
        Assert.isTrue(count==0, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //用户是否填过表单
        wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserBind userBind = this.baseMapper.selectOne(wrapper);
        if(userBind==null){
            //创建用户绑定记录
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVO,userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            this.baseMapper.insert(userBind);
        }else{
            BeanUtils.copyProperties(userBindVO,userBind);
            this.baseMapper.updateById(userBind);
        }

        //组装自动提交表单的参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard",userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));

        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return formStr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(Map<String, Object> paramMap) {
        String bindCode = (String) paramMap.get("bindCode");
        String userId = (String) paramMap.get("agentUserId");
        UserBind userBind = baseMapper.selectOne(new QueryWrapper<UserBind>().eq("user_id", userId));
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        //更新用户绑定表
        baseMapper.updateById(userBind);
        //更新用户表
        UserInfo userInfo = userInfoService.getById(userId);
        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoService.updateById(userInfo);
    }
}
